package com.example.administrator.dc.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.example.administrator.dc.Bean.Carousel;
import com.example.administrator.dc.Bean.School;
import com.example.administrator.dc.R;
import com.example.administrator.dc.adapter.UserListAdapter;
import com.example.administrator.dc.model.CarouselModel;
import com.example.administrator.dc.model.UsersModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

import static com.example.administrator.dc.helper.Carousel.setCarousel;

/**
 * A simple {@link Fragment} subclass.
 */
public class CampusFragment extends Fragment {

    private RecyclerView recyclerView;
    //private RecyclerViewHeader header;
    private View view=null;
    //private UserListAdapter mAdapter;
    // private  List<String> mDatas=new ArrayList<>();


    private Context context;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private String[] image = {"company1.jpg","company2.jpg","company3.jpg","company4.jpg"};
    private List<String> images =new ArrayList<>();
    // private List<String> imgUrl =new ArrayList<>();
    // String imgUrl;
    String imgUrl="http://123.207.174.155/zqb/Public/goodsImg/";
    public CampusFragment() {
        // Required empty public constructor
    }








    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_campus, container, false);
        initViews();
        initView();
        getData();
        return  view;
    }
    private void initViews(){
        recyclerView=(RecyclerView)view.findViewById(R.id.myhome_recycleview_id);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        // mAdapter = new MyAdapter(this,mDatas , R.layout.head_reacycleview);

        GridLayoutManager gm = new GridLayoutManager(this.getActivity(),2);
        // 网格是的list布局   2列
        // recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setLayoutManager(gm);
        // recyclerView.setAdapter(mAdapter);
        // header.attachTo(recyclerView);


    }
    private void initView() {
       /* for(int i=0;i<4;i++){
           images.add(imgUrl+image[i]);
        }
        context=getActivity();
        li=(LinearLayout)view.findViewById(R.id.play);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
        Display d=getActivity().getWindowManager().getDefaultDisplay();
                *//*获取布局参数*//*
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);*/
        CarouselModel carouselModel=new  CarouselModel();
        Call<List<Carousel>> call1=carouselModel.Carousel();
        call1.enqueue(new Callback<List<Carousel>>() {
            @Override
            public void onResponse(Call<List<Carousel>> call1, retrofit2.Response<List<Carousel>> response)
            {
                List<Carousel> res=response.body();
                for (int i=0;i<res.size();i++)
                {
                    images.add(res.get(i).getPhoto());
                }
                context=getActivity();
                li=(LinearLayout)view.findViewById(R.id.play);
                // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
                convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
                Display d=getActivity().getWindowManager().getDefaultDisplay();
               /*获取布局参数*/
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);

            }
            @Override
            public void onFailure(Call<List<Carousel>> call, Throwable t) {

            }
        });

    }
    private void getData(){
        //获取网上的数据/
      /* *//* Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://60.205.189.39/ ")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ShopService testService=retrofit.create(ShopService.class);
        Call<List<Shop>> call=testService.getAllShop();*//*
        UsersModel shopModel=new  UsersModel();
        Call<List<School>> call=shopModel.userlist();

        call.enqueue(new Callback<List<School>>() {
            @Override
            public void onResponse(Call<List<School>> call, retrofit2.Response<List<School>> response)
            {
                List<School> shop=response.body();

                recyclerView.setAdapter(new UserListAdapter(getActivity(),shop));
            }
            @Override
            public void onFailure(Call<List<School>> call, Throwable t) {

            }
        });*/



    }




}
